<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Sanliuling
 *
 * @author add bydragon.hu@2015.11.30
 */
require_once 'BaseChannel.php';

//oppo
class Sanliuling implements BaseChannel {
	
	function __construct() {
		//echo "construct";
	}

	//put your code here
	function android($data) {
		$paymentKey="";
		$ret = $this->paymentCallBack($paymentKey, ANDROID);
		echo $ret;
	}

	function ios($data) {
		$paymentKey="";
		$ret = $this->paymentCallBack($paymentKey, IOS);
		echo $ret;
	}

	function paymentCallBack($paymentKey, $platform)
	{
		require_once 'include/sanliuling/common.inc.php';
		
		$orderLog = new ChargeOrder();
		$orderLog->log("sanliuling", $_REQUEST);
		
		//TODO::实现自己的App逻辑后，替换此处代码
		$myApp = new PayApp_Demo(QIHOO_APP_KEY, QIHOO_APP_SECRET);
		$qihooPay = new Qihoo_Pay($myApp);
		//$qihooPay->processRequest();
		
		//1.支付完成后, 360服务器会把支付结果以 GET 方式通知到此地址 (建议应用服务端接口同时支持 GET 和 POST). 
		$params = $_REQUEST;
		$qihooPay->setRequest($params);
		
		if ($params['app_key'] != QIHOO_APP_KEY) {
			echo "not my request, I am " . QIHOO_APP_KEY;
			$orderLog->log("sanliuling", "app_key:".$params['app_key']." failure");
			return;
		}
		
		if (!$qihooPay->_isValidRequest($params)) {  //包含sign验证的过程在里面
			echo 'invalid request';
			$orderLog->log("sanliuling", "sign:".$params['sign']." failure");
			return;
		}
		
		$verfifyRet = $qihooPay->_verifyOrder($params);
		if ($verfifyRet != Qihoo_Pay::VERIFIED) {
			echo $verfifyRet;
			$orderLog->log("sanliuling", "verify order failed! orderid:".$params['order_id']);
			return;
		}
		
		//支付扩展信息
		if (!empty($params['pay_ext'])) {
			$params['pay_ext'] = json_decode($params['pay_ext'], 1);
		}
		
		//if ($this->_payApp->isValidOrder($params)) {
		//	$this->_payApp->processOrder($params);
		//}
		if($params['gateway_flag'] == 'success')
		{
			$status = STATUS_SUCCESS;
		}else
		{
			$status = STATUS_FAILD;
		}
		//收到 的钱是分为单位。。
		if(true == $orderLog->recordOrder($status, $params['amount']/100, $params['app_order_id'], "sanliuling", $platform, $params['order_id']))
		{
			$orderLog->log('sanliuling','platform:'.$platform.'amount:'.($params['amount']/100).' order_id:'.$params['product_id'].' success');
			
		}else
		{
			$orderLog->log('sanliuling','platform:'.$platform.'amount:'.($params['amount']/100).' order_id:'.$params['product_id'].' fail');
		}
		
		echo 'ok';  //回应的 ok 表示应用已经正常接到消息, 无需继续发送通知. 它不表示订单成功与否, 或者应用处理成功与否
		
		//2.应用接收验证参数后,给用户做游戏内充值.
		
		/*3.应用服务端通知接口在接收到通知消息后, 需回应 ok(仅返回小写 ok 这两个字母，不要有其它输出), 表示通知已经接收.
		 如果回应其他值或者不回应, 则被认为通知失败, 360 会尝试多次通知. 这个机制用来避免掉单*/
		
		/*4.对于重复的通知,应用可能发现订单已经成功处理完毕, 无需继续处理, 也要返回 ok(仅返回小写 ok 这两个字母，不要有其
                        它输出). 否则, 360 会认为未成功通知, 会继续发送通知.*/
		
	}	
}